using UnityEngine;


//方向枚举
public enum Direction{
    North,
    East,
    South,
    West
}
//方向改变
public enum DirectionChange{
    None,
    TurnRight,
    TurnLeft,
    TurnAround//掉头
}


//方向扩展
public static class DirectionExtensions
{
    static Quaternion[] rotations = {
        Quaternion.identity,//单位旋转（只读）。
        Quaternion.Euler(0f, 90f, 0f),
        Quaternion.Euler(0f, 180f, 0f),
        Quaternion.Euler(0f, 270f, 0f)
    };

    //扩展方法是静态类内部的静态方法，其行为类似于某种类型的实例方法
    //扩展方法的第一个参数需要具有this关键字。它定义了方法将要操作的类型和实例值
    public static Quaternion GetRotation(this Direction direction)
    {
        return rotations[(int)direction];
    }

    //返回从当前方向到下一个方向的方向更改
    public static DirectionChange GetDirectionChangeTo(this Direction current, Direction next)
    {
        if(current == next){
            return DirectionChange.None;//方向相同，则没有方向
        }else if(current + 1 == next || current - 3 == next){
            return DirectionChange.TurnRight;//下一个比当前多1或者小3，右转
        }else if(current - 1 == next || current + 3 == next){
            return DirectionChange.TurnLeft;//下一个比当前小1或者多3，左转
        }
        return DirectionChange.TurnAround;//其他情况转身
    }

    //以角度为单位获取方向的角度
    public static float GetAngle(this Direction direction)
    {
        return (float)direction * 90f;
    }

    //半向量
    static Vector3[] halfVectors = {
        Vector3.forward * 0.5f,
        Vector3.right * 0.5f,
        Vector3.back * 0.5f,
        Vector3.left * 0.5f
    };

    public static Vector3 GetHalfVector(this Direction direction)
    {
        return halfVectors[(int)direction];
    }
}